package com.hmkj.core.api.ninepai;

import com.hmkj.core.api.ninepai.utils.HiDesUtils;
import com.hmkj.core.api.ninepai.utils.RSASignUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lvyb on 2017/7/4.
 */
@Slf4j
public class RpmUtils extends RpmCase {

    public RpmUtils() {

    }

    public static void main(String[] args) {
        System.out.print(HiDesUtils.desDeCode("4ff3401d425d0a756a1e80486f391746710ef975a9577d9b"));
//        RpmUtils test = new RpmUtils();
//        RpmUtils.doCardSignStatusQuery();
//        RpmUtils.doBankList();
        RpmUtils.doBindCard(1,"","341181198910156610","阿斯蒂","13122222222","6228489877656254152","0","","");
//        RpmUtils.doUnbindCard();
//        RpmUtils.doQuickPaySms();
//        RpmUtils.doQuickPay();
//        RpmUtils.doPayQuery();
//        RpmUtils.doRefund();
//        RpmUtils.doRefundQuery();
//        RpmUtils.doMemberCardList();
//        RpmUtils.doCardInfo();
//        RpmUtils.doQuickPayInit();
//        RpmUtils.doQuickPayCommit();
    }

    private void doCardSignStatusQuery() {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmQueryCardBindStatus");//接口类型
        //1、具体业务请求参数
        paramMap.put("memberId", "12345678");
        paramMap.put("cardNo", HiDesUtils.desEnCode("6222080200011462691"));
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("hasSign", RSASignUtil.getValue(res, "hasSign"));
        retMap.put("contractId", RSASignUtil.getValue(res, "contractId"));
        validRetSign(retMap, res);
    }

    /**
     * 快捷绑卡
     *
     * @param userId     用户ID
     * @param orderNo    订单号
     * @param idNo       身份证号
     * @param userName   真实姓名
     * @param phone      预留手机
     * @param cardNo     银行卡号
     * @param cardType   银行卡类别 0 储蓄卡  1 信用卡
     * @param expireDate 贷记卡有效期
     * @param cvn2       贷记卡cnv2
     * @return
     */
    public static Map<String, String> doBindCard(Integer userId, String orderNo, String idNo, String userName, String phone, String cardNo, String cardType, String expireDate, String cvn2) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmBindCard");//接口类型
        paramMap.put("memberId", userId.toString());
        paramMap.put("orderId", orderNo);
        paramMap.put("idType", "00");
        paramMap.put("idNo", HiDesUtils.desEnCode(idNo));
        paramMap.put("userName", userName);
        paramMap.put("phone", phone);
        paramMap.put("cardNo", HiDesUtils.desEnCode(cardNo));
        paramMap.put("cardType", cardType);
        paramMap.put("expireDate", expireDate);
        paramMap.put("cvn2", cvn2);
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("contractId", RSASignUtil.getValue(res, "contractId"));
        retMap.put("orderId", RSASignUtil.getValue(res, "orderId"));
        retMap.put("bankName", RSASignUtil.getValue(res, "bankName"));
        retMap.put("bankAbbr", RSASignUtil.getValue(res, "bankAbbr"));
        retMap.put("cardType", RSASignUtil.getValue(res, "cardType"));
        retMap.put("extension", RSASignUtil.getValue(res, "extension"));
        validRetSign(retMap, res);
        return retMap;
    }

    /**
     * 银行卡解绑
     *
     * @param userId     用户ID
     * @param contractId 绑卡协议号
     * @return
     */
    public static Map<String, String> doUnbindCard(Integer userId, String contractId) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmUnbindCard");//接口类型
        //1、具体业务请求参数
        paramMap.put("contractId", contractId);
        paramMap.put("memberId", userId.toString());
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("memberId", RSASignUtil.getValue(res, "memberId"));
        validRetSign(retMap, res);
        return retMap;
    }

    private void doQuickPaySms() {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmQuickPaySms");//接口类型
        //1、具体业务请求参数
        paramMap.put("contractId", "201704230000016578");
        paramMap.put("memberId", "110000002601");
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("phone", RSASignUtil.getValue(res, "phone"));
        validRetSign(retMap, res);
    }

    private void doQuickPay() {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmQuickPay");//接口类型/商户自验短信
        //1、具体业务请求参数
        paramMap.put("orderId", DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"));
        paramMap.put("contractId", "201704070000013094");
        paramMap.put("memberId", "123456789");
        paramMap.put("checkCode", "");
        paramMap.put("payType", "DQP");
        paramMap.put("account", "1189");
        paramMap.put("currency", "CNY");
        paramMap.put("orderTime", DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"));
        paramMap.put("clientIP", "100.202.113.87");
        paramMap.put("validUnit", "01");
        paramMap.put("validNum", "1");
        paramMap.put("goodsName", "手机");
        paramMap.put("goodsDesc", "国产手机 华为P10");
        paramMap.put("offlineNotifyUrl", "http://100.66.155.60:8090/payNotice.jsp");
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("orderId", RSASignUtil.getValue(res, "orderId"));
        retMap.put("orderSts", RSASignUtil.getValue(res, "orderSts"));
        retMap.put("payOrderId", RSASignUtil.getValue(res, "payOrderId"));
        retMap.put("acDate", RSASignUtil.getValue(res, "acDate"));
        validRetSign(retMap, res);
    }

    private void doPayQuery() {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmPayQuery");//接口类型
        //1、具体业务请求参数
        paramMap.put("orderId", "20170414153125");
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("memberId", RSASignUtil.getValue(res, "memberId"));
        retMap.put("orderId", RSASignUtil.getValue(res, "orderId"));
        retMap.put("account", RSASignUtil.getValue(res, "account"));
        retMap.put("orderTime", RSASignUtil.getValue(res, "orderTime"));
        retMap.put("payResult", RSASignUtil.getValue(res, "payResult"));
        retMap.put("bankAbbr", RSASignUtil.getValue(res, "bankAbbr"));
        retMap.put("payTime", RSASignUtil.getValue(res, "payTime"));
        retMap.put("payOrderId", RSASignUtil.getValue(res, "payOrderId"));
        retMap.put("acDate", RSASignUtil.getValue(res, "acDate"));
        retMap.put("fee", RSASignUtil.getValue(res, "fee"));
        validRetSign(retMap, res);
    }

    private void doRefund() {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmRefund");//接口类型
        //1、具体业务请求参数
        paramMap.put("refundAmount", "10");
        paramMap.put("oriOrderId", "20170427176427119");
        paramMap.put("orderId", "20170428222403001");
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("refundAmount", RSASignUtil.getValue(res, "refundAmount"));
        retMap.put("orderId", RSASignUtil.getValue(res, "orderId"));
        retMap.put("orderSts", RSASignUtil.getValue(res, "orderSts"));
        validRetSign(retMap, res);
    }

    private void doRefundQuery() {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmRefundQuery");//接口类型
        //1、具体业务请求参数
        paramMap.put("oriOrderId", "1493391197196120502");
        paramMap.put("orderId", "20170428222433001");
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("refundAmount", RSASignUtil.getValue(res, "refundAmount"));
        retMap.put("orderId", RSASignUtil.getValue(res, "orderId"));
        retMap.put("orderSts", RSASignUtil.getValue(res, "orderSts"));
        validRetSign(retMap, res);
    }

    private void doBankList() {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmBankList");//接口类型
        //1、具体业务请求参数

        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("bankList", RSASignUtil.getValue(res, "bankList"));
        retMap.put("creditBankList", RSASignUtil.getValue(res, "creditBankList"));//网关返回的还是加密的
        validRetSign(retMap, res);
    }

    private void doMemberCardList() {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmMemberCardList");//接口类型
        //1、具体业务请求参数
        paramMap.put("memberId", "123456789");
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("memberId", RSASignUtil.getValue(res, "memberId"));
        retMap.put("cardList", RSASignUtil.getValue(res, "cardList"));
        validRetSign(retMap, res);
    }

    private void doCardInfo() {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("service", "rpmCardInfo");//接口类型
        //1、具体业务请求参数
        paramMap.put("cardNo", HiDesUtils.desEnCode("6225880154901171"));
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("cardNo", RSASignUtil.getValue(res, "cardNo"));
        retMap.put("cardType", RSASignUtil.getValue(res, "cardType"));
        retMap.put("bankName", RSASignUtil.getValue(res, "bankName"));
        retMap.put("bankAbbr", RSASignUtil.getValue(res, "bankAbbr"));
        validRetSign(retMap, res);
    }

    /**
     * 快捷支付预下单（发送短信验证码）
     *
     * @param paramMap 支付参数
     * @return
     */
    public static Map<String, String> doQuickPayInit(Map<String, String> paramMap) {
        //1、具体业务请求参数
        paramMap.put("service", "rpmQuickPayInit");//接口类型、快捷支付发起（我方验证短信）

        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("orderId", RSASignUtil.getValue(res, "orderId"));
        retMap.put("payOrderId", RSASignUtil.getValue(res, "payOrderId"));
        retMap.put("acDate", RSASignUtil.getValue(res, "acDate"));
        validRetSign(retMap, res);
        return retMap;
    }

    /**
     * 快捷支付提交
     *
     * @param paramMap  支付参数
     * @param checkCode 短信验证码
     * @return
     */
    public static Map<String, String> doQuickPayCommit(Map<String, String> paramMap, String checkCode) {
        //1、具体业务请求参数
        paramMap.put("service", "rpmQuickPayCommit");//接口类型、快捷支付发起（我方验证短信）
        paramMap.put("checkCode", checkCode);
        //2、请求流程
        String res = bizService(paramMap);
        //3、验证返回签名
        Map<String, String> retMap = new HashMap<>();
        retMap.put("orderId", RSASignUtil.getValue(res, "orderId"));
        retMap.put("orderSts", RSASignUtil.getValue(res, "orderSts"));
        retMap.put("payOrderId", RSASignUtil.getValue(res, "payOrderId"));
        retMap.put("acDate", RSASignUtil.getValue(res, "acDate"));
        validRetSign(retMap, res);
        return retMap;
    }

    /**
     * 组装快捷支付参数
     *
     * @param userId     用户ID
     * @param orderNo    订单号
     * @param contractId 绑卡协议号
     * @param amount     金额
     * @param orderTime  订单日期
     * @param clientIP   客户端IP
     * @param notifyUrl  异步回调地址
     * @return
     */
    public static Map<String, String> buildQuickPayParams(Integer userId, String orderNo, String contractId, BigDecimal amount, Date orderTime, String clientIP, String notifyUrl) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("memberId", userId.toString());
        paramMap.put("orderId", orderNo);
        paramMap.put("contractId", contractId);
        paramMap.put("payType", "DQP");
        paramMap.put("currency", "CNY");
        paramMap.put("orderTime", DateFormatUtils.format(orderTime, "yyyyMMddHHmmss"));
        paramMap.put("clientIP", clientIP);
        paramMap.put("validUnit", "01");
        paramMap.put("validNum", "24");
        paramMap.put("goodsName", "用户充值");
        paramMap.put("goodsDesc", "用户充值");
        paramMap.put("account", String.valueOf(amount.multiply(new BigDecimal(100)).intValue()));
        paramMap.put("offlineNotifyUrl", notifyUrl);
        return paramMap;
    }

}
